/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  统一发布活动
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnityStartSessionPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidItemManager bidItemManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String sessionId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		log.info("开始统一发布活动，批量发布标的，并且加载缓存...");
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(sessionId)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数，活动发布失败");
			return entity;
		}
		ParamsVo<BidSession> svo = new ParamsVo<BidSession>();
		svo.setInfoId(sessionId);
		BidSession session = (BidSession) bidSessionManager.getInfoById(svo);
		if (Validator.isEmpty(session)) {
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage("没有获取待发布的活动，活动发布失败");
			return entity;
		}
		String flag = UnityItemPluginHelper.getInstance().checkPublish("", sessionId);
		if (Validator.isNotNull(flag)) {//判断是否有发布的条件
			entity.setResult("false");
			entity.setEntity("false");
			entity.setMessage(flag);
			return entity;
		}
		String status = session.getStatus();
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
		session.setStatus(Constant.getInstance().getSessionConfig("statusPubslish"));
		cache.createCacheObject(session);

		ParamsVo<BidItem> ivo = new ParamsVo<BidItem>();
		BidItem item = new BidItem();
		item.setSessionId(sessionId);
		ivo.setObj(item);
		ivo.setMethodKey("ByCacheList");
		List list = this.bidItemManager.getList(ivo);
		if (!Validator.isEmpty(list)) {
			int count = list.size();
			for (int i = 0; i < count; i++) {
				BidItemCacheVo cacheVo = (BidItemCacheVo) list.get(i);
				if (!Constant.getInstance().getItemConfig("statusPubslish").equals(cacheVo.getItemStatus())) {
					UnityItemPluginHelper.getInstance().startItem(cacheVo, false);
				}
			}
		}

		svo.setObj(session);
		boolean mark = bidSessionManager.editInfo(svo);
		if (mark) {
			//更新该活动下所有（待审核、审核中）中标的状态=已发布
			ParamsVo<BidItem> itemVo = new ParamsVo<BidItem>();
			itemVo.setParams("itemStatus", Constant.getInstance().getItemConfig("statusPubslish"));//<!-- 改状态2：已发布 -->
			itemVo.setParams("sessionId", sessionId);
			//设置要满足状态更新的历史状态标的
			itemVo.setParams("itemStatusStr", new String[] { Constant.getInstance().getItemConfig("statusTemp"),//0待审核
					Constant.getInstance().getItemConfig("statusAudit"), //1审核中
					Constant.getInstance().getItemConfig("statusSuspend") //3已暂停
					});
			itemVo.setMethodKey("BidItemStatus");
			bidItemManager.editInfo(itemVo);
			entity.setMessage("操作成功");
			entity.setEntity("true");
			entity.setResult("true");
		} else {
			session.setStatus(status);
			cache.updateCacheObject(session);
			entity.setEntity("false");
			entity.setResult("false");
			entity.setMessage("活动发布失败");
		}
		return entity;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

}
